package com.oceansoft.mobile.biz.api.service;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.biz.admin.entity.Wxjq;
import com.oceansoft.mobile.biz.admin.entity.WxjqMapper;
import com.oceansoft.mobile.biz.api.entity.WxDept;
import com.oceansoft.mobile.biz.api.entity.WxDeptResevre;
import com.oceansoft.mobile.biz.api.entity.WxReserve;
import com.oceansoft.mobile.common.base.entity.Pager;
import com.oceansoft.mobile.common.base.entity.Query;
import com.oceansoft.mobile.common.dao.ICommonDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/27.
 */
@Service
public class ApiHzyyServiceImpl implements IApiHzyyService {
    @Resource
    private ICommonDao commonDao;

    @Override
    public void saveDept(JSONObject wxdept,String parentid) {
        commonDao
                .updateOne(
                        "INSERT INTO t_wx_dept (orgid,orgname,type,parentid) VALUES('" + wxdept.get("ORGID")+ "','" +  wxdept.get("ORGNAME") + "','"+ wxdept.get("Type")+"','"+parentid+"')",
                        wxdept);
    }

    @Override
    public int isHoliday(String date) {
       return commonDao.getCount("select isHoliday('"+date+"')from dual");
    }

    @Override
    public List<WxDept> getDept(String parentid) {
        return commonDao.loadList("select orgid,orgname from t_wx_dept T where T.parentid  = ?",
                WxDept.getRowMapper(), parentid);
    }

    @Override
    public WxDeptResevre getRevnum(String orgid) {
        return commonDao.loadOne("select * from t_wx_deptreserve T where T.DEPTID  = ?",
                WxDeptResevre.getRowMapper(),orgid);
    }

    @Override
    public WxDeptResevre getsumnumBydate(String date,String deptid) {
        return commonDao.loadOne("select sum(sbnum) as sbnum,sum(zxnum) as zxnum,sum(qynum) as qynum,sum(djnum) as djnum,'' as deptid  from t_wx_reserve T where T.date  = '"+date+"' and deptid = '"+deptid+"'",
                WxDeptResevre.getRowMapper());
    }

    @Override
    public int checkIsRev(String date, String orgid) {
        return commonDao.getCount("select count(1)  from t_wx_reserve where date = '"+date+"' and deptid ='"+orgid+"'");
    }

    @Override
    public boolean insertRev(WxReserve wxReserve) {
        int i =commonDao
                .updateOne(
                        "INSERT INTO t_wx_reserve (deptid,date,sbnum,zxnum,qynum,djnum,openid,xm,bjbh,lxdh,sfzh,userguid,createdate) VALUES('"+wxReserve.getDeptid()+"','"+wxReserve.getDate().replaceAll("-","")+"',"+wxReserve.getSbnum()+","+wxReserve.getZxnum()+","+wxReserve.getQynum()+","+wxReserve.getDjnum()+",'"+wxReserve.getOpenId()+"','"+wxReserve.getXm()+"','"+wxReserve.getBjbh()+"','"+wxReserve.getLxdh()+"','"+wxReserve.getSfzh()+"','"+wxReserve.getUserguid()+"','"+wxReserve.getCreatedate().replaceAll("-","")+"')"
                        );
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public Pager getWxReserve(String openid, Query query) {
        String page_sql =
                "SELECT t.deptid,DATE_FORMAT(t.date,'%Y-%m-%d ') as date,t.sbnum,t.zxnum,t.qynum,t.djnum,t.openId,t.xm,DATE_FORMAT(t.createdate,'%Y-%m-%d ') as createdate,t.bjbh,t.lxdh,d.orgname FROM  t_wx_reserve t,t_wx_dept d  WHERE t.deptid=d.orgid and t.userguid ='"+openid+"' order by t.createdate desc";
        String count=page_sql;
        page_sql+= Query.getPageSQL("T", query);
        String count_sql =
                "SELECT COUNT(*) FROM ( "+count+" ) b";
        List list = commonDao.loadMapList(page_sql);
        int total = commonDao.getCount(count_sql);
        return new Pager(total, query.getPage(), list);
    }

    @Override
    public WxDeptResevre getsumnumBydateAndOpenId(String date, String orgid, String openId) {
        String sql ="select sum(sbnum) as issb,sum(zxnum) as iszx,sum(qynum) as isqy,sum(djnum) as isdj from t_wx_reserve T where T.date  = '"+date+"' and t.userguid='"+openId+"'";
        return commonDao.loadOne(sql,
                WxDeptResevre.getRowMapperByOpenId());
    }

    @Override
    public boolean checkRecByopenId(WxReserve wxReserve) {
        String sql ="select count(1)  from t_wx_reserve where userguid = '" + wxReserve.getUserguid() + "' and  date =  '" + wxReserve.getDate().replaceAll("-","")+"'and ";
        if("0".equals(wxReserve.getType())){
            sql+=" sbnum =1";
        }else if("1".equals(wxReserve.getType())){
            sql+=" qynum =1";
        }
        else if("2".equals(wxReserve.getType())){
            sql+=" djnum =1";
        }
        else if("3".equals(wxReserve.getType())){
            sql+=" zxnum =1";
        }
        int i =commonDao.getCount(sql);
        if(i>0){
            return false;
        }
        return true;
    }
}
